package c.c.a;

import android.app.Application;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.HandlerThread;
import android.os.Message;
import android.util.Log;
import c.b.a.a.a;
import c.c.a.y1.b;
import com.huawei.agconnect.exception.AGCServerException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;

public class s1 implements Callback, Comparator<s> {
    public static s1 k;
    public o1 a;
    public boolean b;
    public Application c;
    public g2 d;
    public final ArrayList<s> e = new ArrayList(32);
    public u f;
    public c g;
    public Handler h;
    public y1 i;
    public Handler j;

    public static void a(s sVar) {
        s1 s1Var = k;
        if (s1Var == null) {
            Log.w("TeaLog", "Init comes First!", null);
            LinkedList linkedList = a2.a;
            synchronized (linkedList) {
                if (linkedList.size() > AGCServerException.OK) {
                    s sVar2 = (s) linkedList.poll();
                    Log.w("TeaLog", "drop event in cache", null);
                    a2.b.add(sVar2);
                }
                linkedList.add(sVar);
            }
            return;
        }
        int size;
        if (sVar.a == 0) {
            m0.b(null);
        }
        if (sVar instanceof b0) {
            ((b0) sVar).k = s1Var.d.a();
        }
        synchronized (s1Var.e) {
            size = s1Var.e.size();
            s1Var.e.add(sVar);
        }
        if (size % 10 == 0) {
            Handler handler = s1Var.j;
            if (handler != null) {
                handler.removeMessages(4);
                s1Var.j.sendEmptyMessageDelayed(4, size == 0 ? 500 : 250);
            }
        }
    }

    public static s1 b() {
        if (k == null) {
            synchronized (s1.class) {
                if (k == null) {
                    k = new s1();
                }
            }
        }
        return k;
    }

    public static y1 d() {
        s1 s1Var = k;
        if (s1Var != null) {
            return s1Var.i;
        }
        m0.b(null);
        return null;
    }

    /* JADX WARNING: Removed duplicated region for block: B:62:0x0158  */
    /* JADX WARNING: Removed duplicated region for block: B:41:0x00e3  */
    /* JADX WARNING: Removed duplicated region for block: B:66:0x0161  */
    /* JADX WARNING: Removed duplicated region for block: B:70:0x0191  */
    /* JADX WARNING: Removed duplicated region for block: B:78:0x01f0  */
    /* JADX WARNING: Removed duplicated region for block: B:77:0x01e9  */
    /* JADX WARNING: Removed duplicated region for block: B:41:0x00e3  */
    /* JADX WARNING: Removed duplicated region for block: B:62:0x0158  */
    /* JADX WARNING: Removed duplicated region for block: B:66:0x0161  */
    /* JADX WARNING: Removed duplicated region for block: B:70:0x0191  */
    /* JADX WARNING: Removed duplicated region for block: B:77:0x01e9  */
    /* JADX WARNING: Removed duplicated region for block: B:78:0x01f0  */
    /* JADX WARNING: Removed duplicated region for block: B:62:0x0158  */
    /* JADX WARNING: Removed duplicated region for block: B:41:0x00e3  */
    /* JADX WARNING: Removed duplicated region for block: B:66:0x0161  */
    /* JADX WARNING: Removed duplicated region for block: B:70:0x0191  */
    /* JADX WARNING: Removed duplicated region for block: B:78:0x01f0  */
    /* JADX WARNING: Removed duplicated region for block: B:77:0x01e9  */
    /* JADX WARNING: Missing block: B:99:0x0268, code skipped:
            if (r3 != null) goto L_0x026a;
     */
    public final void c(java.lang.String[] r20) {
        /*
        r19 = this;
        r1 = r19;
        r2 = r20;
        r3 = r1.e;
        monitor-enter(r3);
        r0 = r1.e;	 Catch:{ all -> 0x02f6 }
        r0 = r0.clone();	 Catch:{ all -> 0x02f6 }
        r4 = r0;
        r4 = (java.util.ArrayList) r4;	 Catch:{ all -> 0x02f6 }
        r0 = r1.e;	 Catch:{ all -> 0x02f6 }
        r0.clear();	 Catch:{ all -> 0x02f6 }
        monitor-exit(r3);	 Catch:{ all -> 0x02f6 }
        if (r2 == 0) goto L_0x0053;
    L_0x0018:
        r0 = r4.size();
        r3 = r2.length;
        r0 = r0 + r3;
        r4.ensureCapacity(r0);
        r3 = r2.length;
        r0 = 0;
        r5 = 0;
    L_0x0024:
        if (r5 >= r3) goto L_0x0053;
    L_0x0026:
        r0 = r2[r5];
        r6 = c.c.a.s.h;
        r6 = new org.json.JSONObject;	 Catch:{ all -> 0x0048 }
        r6.<init>(r0);	 Catch:{ all -> 0x0048 }
        r0 = c.c.a.u.e;	 Catch:{ all -> 0x0048 }
        r7 = "k_cls";
        r8 = "";
        r7 = r6.optString(r7, r8);	 Catch:{ all -> 0x0048 }
        r0 = r0.get(r7);	 Catch:{ all -> 0x0048 }
        r0 = (c.c.a.s) r0;	 Catch:{ all -> 0x0048 }
        r0 = r0.clone();	 Catch:{ all -> 0x0048 }
        r0 = r0.f(r6);	 Catch:{ all -> 0x0048 }
        goto L_0x004d;
    L_0x0048:
        r0 = move-exception;
        c.c.a.m0.b(r0);
        r0 = 0;
    L_0x004d:
        r4.add(r0);
        r5 = r5 + 1;
        goto L_0x0024;
    L_0x0053:
        r0 = r1.d;
        java.util.Objects.requireNonNull(r0);
        r0 = r4.size();
        if (r0 <= 0) goto L_0x02f5;
    L_0x005e:
        r0 = r1.d;
        r0 = r0.b();
        if (r0 == 0) goto L_0x0299;
    L_0x0066:
        java.util.Collections.sort(r4, r1);
        r2 = new java.util.ArrayList;
        r0 = r4.size();
        r2.<init>(r0);
        r0 = r4.iterator();
        r3 = 1;
        r4 = 0;
        r5 = 0;
    L_0x0079:
        r6 = r0.hasNext();
        r7 = 30000; // 0x7530 float:4.2039E-41 double:1.4822E-319;
        if (r6 == 0) goto L_0x01f5;
    L_0x0081:
        r6 = r0.next();
        r6 = (c.c.a.s) r6;
        r9 = r1.i;
        java.util.Objects.requireNonNull(r9);
        r10 = r6 instanceof c.c.a.a0;
        r11 = c.c.a.y1.d(r6);
        r12 = r9.h;
        r14 = -1;
        r16 = 0;
        r18 = (r12 > r14 ? 1 : (r12 == r14 ? 0 : -1));
        if (r18 != 0) goto L_0x00a6;
    L_0x009c:
        r3 = c.c.a.y1.d(r6);
        r9.c(r6, r2, r3);
    L_0x00a3:
        r20 = r0;
        goto L_0x00de;
    L_0x00a6:
        r12 = r9.i;
        if (r12 != 0) goto L_0x00b0;
    L_0x00aa:
        if (r11 == 0) goto L_0x00b0;
    L_0x00ac:
        r9.c(r6, r2, r3);
        goto L_0x00a3;
    L_0x00b0:
        r12 = r9.j;
        r3 = (r12 > r16 ? 1 : (r12 == r16 ? 0 : -1));
        if (r3 == 0) goto L_0x00cd;
    L_0x00b6:
        r14 = r6.a;
        r3 = r9.a;
        r3 = r3.e;
        r20 = r0;
        r0 = "session_interval";
        r7 = r3.getLong(r0, r7);
        r7 = r7 + r12;
        r0 = (r14 > r7 ? 1 : (r14 == r7 ? 0 : -1));
        if (r0 <= 0) goto L_0x00cf;
    L_0x00c9:
        r9.c(r6, r2, r11);
        goto L_0x00de;
    L_0x00cd:
        r20 = r0;
    L_0x00cf:
        r7 = r9.h;
        r12 = r6.a;
        r14 = 7200000; // 0x6ddd00 float:1.0089349E-38 double:3.5572727E-317;
        r12 = r12 + r14;
        r0 = (r7 > r12 ? 1 : (r7 == r12 ? 0 : -1));
        if (r0 <= 0) goto L_0x00e0;
    L_0x00db:
        r9.c(r6, r2, r11);
    L_0x00de:
        r0 = 1;
        goto L_0x00e1;
    L_0x00e0:
        r0 = 0;
    L_0x00e1:
        if (r10 == 0) goto L_0x0158;
    L_0x00e3:
        r3 = r6;
        r3 = (c.c.a.a0) r3;
        r7 = r3.m();
        if (r7 == 0) goto L_0x012d;
    L_0x00ec:
        r7 = r6.a;
        r9.f = r7;
        r7 = 0;
        r9.j = r7;
        r2.add(r6);
        r7 = r3.j;
        r7 = android.text.TextUtils.isEmpty(r7);
        if (r7 == 0) goto L_0x015f;
    L_0x00ff:
        r7 = r9.d;
        if (r7 == 0) goto L_0x0116;
    L_0x0103:
        r11 = r3.a;
        r13 = r7.a;
        r11 = r11 - r13;
        r13 = r7.i;
        r11 = r11 - r13;
        r13 = 500; // 0x1f4 float:7.0E-43 double:2.47E-321;
        r8 = (r11 > r13 ? 1 : (r11 == r13 ? 0 : -1));
        if (r8 >= 0) goto L_0x0116;
    L_0x0111:
        r7 = r7.k;
        r3.j = r7;
        goto L_0x015f;
    L_0x0116:
        r7 = r9.c;
        if (r7 == 0) goto L_0x015f;
    L_0x011a:
        r11 = r3.a;
        r13 = r7.a;
        r11 = r11 - r13;
        r13 = r7.i;
        r11 = r11 - r13;
        r13 = 500; // 0x1f4 float:7.0E-43 double:2.47E-321;
        r8 = (r11 > r13 ? 1 : (r11 == r13 ? 0 : -1));
        if (r8 >= 0) goto L_0x015f;
    L_0x0128:
        r7 = r7.k;
        r3.j = r7;
        goto L_0x015f;
    L_0x012d:
        r7 = r6.a;
        r11 = 0;
        r7 = r9.b(r7, r11);
        if (r7 == 0) goto L_0x013c;
    L_0x0137:
        r8 = "play_session";
        com.bytedance.embedapplog.AppLog.onEventV3(r8, r7);
    L_0x013c:
        r9.f = r11;
        r7 = r3.a;
        r9.j = r7;
        r2.add(r6);
        r7 = r3.k;
        r8 = ":";
        r7 = r7.contains(r8);
        if (r7 == 0) goto L_0x0152;
    L_0x014f:
        r9.c = r3;
        goto L_0x015f;
    L_0x0152:
        r9.d = r3;
        r3 = 0;
        r9.c = r3;
        goto L_0x015f;
    L_0x0158:
        r3 = r6 instanceof c.c.a.y1.b;
        if (r3 != 0) goto L_0x015f;
    L_0x015c:
        r2.add(r6);
    L_0x015f:
        if (r6 == 0) goto L_0x018f;
    L_0x0161:
        r3 = r9.b;
        r3 = r3.d;
        r7 = "user_unique_id";
        r8 = "";
        r3 = r3.optString(r7, r8);
        r6.d = r3;
        r3 = r9.e;
        r6.c = r3;
        r3 = r9.a;
        r7 = c.c.a.y1.a(r3);
        r6.b = r7;
        r3 = r9.a;
        r3 = r3.e();
        if (r3 == 0) goto L_0x018f;
    L_0x0183:
        r3 = com.bytedance.embedapplog.AppLog.getAbConfigVersion();
        r6.e = r3;
        r3 = com.bytedance.embedapplog.AppLog.getAbSDKVersion();
        r6.f = r3;
    L_0x018f:
        if (r0 == 0) goto L_0x01e7;
    L_0x0191:
        r0 = c.c.a.m0.b;
        if (r0 == 0) goto L_0x01b6;
    L_0x0195:
        r0 = "packAndSend once, ";
        r0 = c.b.a.a.a.g(r0);
        r3 = r1.i;
        r3 = r3.e;
        r0.append(r3);
        r3 = ", hadUI:";
        r0.append(r3);
        r3 = r1.i;
        r3 = r3.i;
        r0.append(r3);
        r0 = r0.toString();
        r3 = 0;
        c.c.a.m0.a(r0, r3);
    L_0x01b6:
        r0 = r1.h;
        if (r0 == 0) goto L_0x01e7;
    L_0x01ba:
        r3 = r1.j;
        r7 = new c.c.a.t1;
        r8 = r1.c;
        r9 = r1.g;
        r11 = r1.f;
        r7.m7912init(r8, r9, r11);
        r8 = 6;
        r3 = r3.obtainMessage(r8, r7);
        r0.sendMessage(r3);
        r0 = r1.h;
        r3 = r1.j;
        r7 = new c.c.a.u1;
        r9 = r1.c;
        r11 = r1.f;
        r12 = r1.d;
        r13 = r1.g;
        r7.m7914init(r9, r11, r12, r13);
        r3 = r3.obtainMessage(r8, r7);
        r0.sendMessage(r3);
    L_0x01e7:
        if (r10 == 0) goto L_0x01f0;
    L_0x01e9:
        r5 = c.c.a.y1.d(r6);
        r3 = 1;
        r4 = 1;
        goto L_0x01f1;
    L_0x01f0:
        r3 = 1;
    L_0x01f1:
        r0 = r20;
        goto L_0x0079;
    L_0x01f5:
        if (r4 == 0) goto L_0x020f;
    L_0x01f7:
        r0 = 7;
        if (r5 == 0) goto L_0x0200;
    L_0x01fa:
        r3 = r1.j;
        r3.removeMessages(r0);
        goto L_0x020f;
    L_0x0200:
        r3 = r1.j;
        r4 = r1.d;
        r4 = r4.e;
        r5 = "session_interval";
        r4 = r4.getLong(r5, r7);
        r3.sendEmptyMessageDelayed(r0, r4);
    L_0x020f:
        r0 = r1.f;
        java.util.Objects.requireNonNull(r0);
        r3 = new java.lang.StringBuilder;
        r3.<init>();
        r4 = "save, ";
        r3.append(r4);
        r4 = r2.toString();
        r3.append(r4);
        r3 = r3.toString();
        r4 = 0;
        c.c.a.m0.a(r3, r4);
        r0 = r0.c;	 Catch:{ all -> 0x025a }
        r3 = r0.getWritableDatabase();	 Catch:{ all -> 0x025a }
        r3.beginTransaction();	 Catch:{ all -> 0x0258 }
        r0 = r2.iterator();	 Catch:{ all -> 0x0258 }
        r4 = 0;
    L_0x023b:
        r5 = r0.hasNext();	 Catch:{ all -> 0x0258 }
        if (r5 == 0) goto L_0x0254;
    L_0x0241:
        r5 = r0.next();	 Catch:{ all -> 0x0258 }
        r5 = (c.c.a.s) r5;	 Catch:{ all -> 0x0258 }
        r6 = r5.i();	 Catch:{ all -> 0x0258 }
        r4 = r5.e(r4);	 Catch:{ all -> 0x0258 }
        r5 = 0;
        r3.insert(r6, r5, r4);	 Catch:{ all -> 0x0258 }
        goto L_0x023b;
    L_0x0254:
        r3.setTransactionSuccessful();	 Catch:{ all -> 0x0258 }
        goto L_0x026a;
    L_0x0258:
        r0 = move-exception;
        goto L_0x025c;
    L_0x025a:
        r0 = move-exception;
        r3 = 0;
    L_0x025c:
        r4 = c.c.a.u.d;	 Catch:{ all -> 0x028b }
        r2 = r2.size();	 Catch:{ all -> 0x028b }
        r4 = r4 + r2;
        c.c.a.u.d = r4;	 Catch:{ all -> 0x028b }
        c.c.a.m0.b(r0);	 Catch:{ all -> 0x028b }
        if (r3 == 0) goto L_0x0273;
    L_0x026a:
        r3.endTransaction();	 Catch:{ all -> 0x026e }
        goto L_0x0273;
    L_0x026e:
        r0 = move-exception;
        r2 = r0;
        c.c.a.m0.b(r2);
    L_0x0273:
        r0 = r1.b;
        if (r0 != 0) goto L_0x02f5;
    L_0x0277:
        r0 = r1.i;
        r0 = r0.i;
        if (r0 == 0) goto L_0x02f5;
    L_0x027d:
        r0 = r1.h;
        if (r0 == 0) goto L_0x02f5;
    L_0x0281:
        r0 = com.bytedance.embedapplog.AppLog.getAutoActiveState();
        if (r0 == 0) goto L_0x02f5;
    L_0x0287:
        r19.e();
        goto L_0x02f5;
    L_0x028b:
        r0 = move-exception;
        r2 = r0;
        if (r3 == 0) goto L_0x0298;
    L_0x028f:
        r3.endTransaction();	 Catch:{ all -> 0x0293 }
        goto L_0x0298;
    L_0x0293:
        r0 = move-exception;
        r3 = r0;
        c.c.a.m0.b(r3);
    L_0x0298:
        throw r2;
    L_0x0299:
        r2 = new android.content.Intent;
        r0 = r1.c;
        r3 = com.bytedance.embedapplog.collector.Collector.class;
        r2.<init>(r0, r3);
        r3 = r4.size();
        r5 = new java.lang.String[r3];
        r0 = 0;
        r6 = 0;
        r6 = 0;
        r7 = 0;
    L_0x02ac:
        if (r6 >= r3) goto L_0x02dd;
    L_0x02ae:
        r0 = r4.get(r6);
        r0 = (c.c.a.s) r0;
        java.util.Objects.requireNonNull(r0);
        r8 = new org.json.JSONObject;
        r8.<init>();
        r9 = "k_cls";
        r10 = r0.i();	 Catch:{ JSONException -> 0x02c9 }
        r8.put(r9, r10);	 Catch:{ JSONException -> 0x02c9 }
        r0.c(r8);	 Catch:{ JSONException -> 0x02c9 }
        goto L_0x02cd;
    L_0x02c9:
        r0 = move-exception;
        c.c.a.m0.b(r0);
    L_0x02cd:
        r0 = r8.toString();
        r5[r6] = r0;
        r0 = r5[r6];
        r0 = r0.length();
        r7 = r7 + r0;
        r6 = r6 + 1;
        goto L_0x02ac;
    L_0x02dd:
        r0 = 307200; // 0x4b000 float:4.30479E-40 double:1.51777E-318;
        if (r7 < r0) goto L_0x02e6;
    L_0x02e2:
        r0 = 0;
        c.c.a.m0.b(r0);
    L_0x02e6:
        r0 = "EMBED_K_DATA";
        r2.putExtra(r0, r5);
        r0 = r1.c;	 Catch:{ Exception -> 0x02f1 }
        r0.sendBroadcast(r2);	 Catch:{ Exception -> 0x02f1 }
        goto L_0x02f5;
    L_0x02f1:
        r0 = move-exception;
        c.c.a.m0.b(r0);
    L_0x02f5:
        return;
    L_0x02f6:
        r0 = move-exception;
        monitor-exit(r3);	 Catch:{ all -> 0x02f6 }
        throw r0;
        */
        throw new UnsupportedOperationException("Method not decompiled: c.c.a.s1.c(java.lang.String[]):void");
    }

    public int compare(Object obj, Object obj2) {
        int i = ((((s) obj).a - ((s) obj2).a) > 0 ? 1 : ((((s) obj).a - ((s) obj2).a) == 0 ? 0 : -1));
        if (i < 0) {
            return -1;
        }
        return i > 0 ? 1 : 0;
    }

    public boolean e() {
        this.b = true;
        p1 p1Var = new p1(this.c, this.g);
        Handler handler = this.h;
        if (handler == null) {
            return false;
        }
        handler.obtainMessage(6, p1Var).sendToTarget();
        return true;
    }

    public boolean f() {
        s1 s1Var = k;
        if (s1Var == null) {
            return false;
        }
        g2 g2Var = s1Var.d;
        if (g2Var == null || g2Var.b.getPicker() == null) {
            return false;
        }
        return true;
    }

    public final void g() {
        o1 o1Var;
        if (!this.d.e()) {
            o1Var = this.a;
            if (o1Var != null) {
                o1Var.c = true;
                this.a = null;
            }
        } else if (this.a == null) {
            o1Var = new o1(this.c, this.g, this.d);
            this.a = o1Var;
            this.h.obtainMessage(6, o1Var).sendToTarget();
        }
    }

    public boolean handleMessage(Message message) {
        int i = message.what;
        ArrayList arrayList;
        if (i == 1) {
            m0.a = this.d.e.getBoolean("bav_log_collect", false);
            if (!this.g.h()) {
                this.j.removeMessages(1);
                this.j.sendEmptyMessageDelayed(1, 1000);
            } else if (this.d.b()) {
                HandlerThread handlerThread = new HandlerThread("bd_tracker_n");
                handlerThread.start();
                Handler handler = new Handler(handlerThread.getLooper(), this);
                this.h = handler;
                handler.sendEmptyMessage(2);
                if (this.e.size() > 0) {
                    this.j.removeMessages(4);
                    this.j.sendEmptyMessageDelayed(4, 1000);
                }
                Log.i("TeaLog", "net|worker start", null);
            }
            LinkedList linkedList = a2.a;
            linkedList = new LinkedList();
            LinkedList linkedList2 = new LinkedList();
            LinkedList linkedList3 = a2.a;
            synchronized (linkedList3) {
                linkedList.addAll(linkedList3);
                LinkedList linkedList4 = a2.b;
                linkedList2.addAll(linkedList4);
                linkedList3.clear();
                linkedList4.clear();
            }
            while (!linkedList.isEmpty()) {
                a((s) linkedList.poll());
            }
            while (!linkedList2.isEmpty()) {
                s sVar = (s) linkedList2.poll();
                if (sVar instanceof b0) {
                    sVar = (b0) sVar;
                } else {
                    s b0Var;
                    if (sVar instanceof v) {
                        v vVar = (v) sVar;
                        StringBuilder g = a.g("");
                        g.append(vVar.j);
                        g.append(vVar.k);
                        b0Var = new b0(g.toString(), "16", 1);
                    } else if (sVar instanceof x) {
                        b0Var = new b0(((x) sVar).k, "16", 1);
                    } else {
                        sVar = null;
                    }
                    sVar = b0Var;
                }
                a(sVar);
            }
        } else if (i == 2) {
            arrayList = new ArrayList(4);
            arrayList.add(new w1(this.c, this.g, this.i));
            arrayList.add(new r1(this.c, this.g, this.d));
            arrayList.add(new v1(this.c, this.g, this.f));
            arrayList.add(new x1(this.c, this.f, this.d, this.g));
            Iterator it = arrayList.iterator();
            while (it.hasNext()) {
                q1 q1Var = (q1) it.next();
                long f = q1Var.f();
                if (f < 864000000) {
                    this.h.sendMessageDelayed(this.j.obtainMessage(6, q1Var), f);
                }
            }
            g();
        } else if (i == 4) {
            c(null);
        } else if (i == 5) {
            c((String[]) message.obj);
        } else if (i == 6) {
            q1 q1Var2 = (q1) message.obj;
            if (!q1Var2.c) {
                long f2 = q1Var2.f();
                if (f2 < 864000000) {
                    this.h.sendMessageDelayed(this.j.obtainMessage(6, q1Var2), f2);
                }
                g();
            }
        } else if (i != 7) {
            m0.b(null);
        } else {
            synchronized (this.e) {
                arrayList = this.e;
                if (y1.o == null) {
                    y1.o = new b(null);
                }
                y1.o.a = System.currentTimeMillis();
                arrayList.add(y1.o);
            }
            c(null);
        }
        return true;
    }
}
